'use strict';

const BaseService = require('./base');

class UserInfoService extends BaseService {
  get _model() {
    return 'UserInfo';
  }

  async findOne({ id, uid }) {
    const { app } = this;
    const where = this.formatWhere({ exact: { id, uid } });
    const include = [
      {
        model: app.model.User,
        attributes: {
          exclude: app.exclude.concat([ 'password' ]),
        },
      }, {
        model: app.model.Area,
        attributes: {
          exclude: app.exclude,
        },
      }];
    return this._findOne({ where, include });
  }

  async create({ uid }) {
    return this._create({ uid });
  }

  async update({ firstName, secondName, avatar, description, phoneNum, address, areaId, uid }) {
    const body = this.formatWhere({ exact: { firstName, secondName, avatar, description, phoneNum, address, areaId } });
    const where = { uid };
    return this._update({ body, where });
  }

  async findList({ firstName, secondName, phoneNum, address, areaId, prop, sort, pageIndex, pageSize, createdAt }) {
    const { app } = this;
    const where = this.formatWhere({ dim: { firstName, secondName, phoneNum, address }, range: { createdAt }, inOp: { areaId } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.Area,
        attributes: [ 'name' ],
      },
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, order, include });
  }
}

module.exports = UserInfoService;
